package com.rfid.spec;

import com.rfid.transport.BufferPack;

public class TypeEBV {
    public byte[] m_value = new byte[9];
    public long m_len = 0L;

    public void FromDWord(long dw) {
        this.m_len = 1L;
        this.m_value[0] = (byte) (int) (dw & 0x7FL);
        if (dw > 127L) this.m_value[0] = (byte) (this.m_value[0] | 0x80);
        else return;
        this.m_len = 2L;
        dw >>= 7;
        this.m_value[1] = (byte) (int) (dw & 0x7FL);
        if (dw > 127L) this.m_value[1] = (byte) (this.m_value[1] | 0x80);
        else return;
        this.m_len = 3L;
        dw >>= 7;
        this.m_value[2] = (byte) (int) (dw & 0x7FL);
        if (dw > 127L) this.m_value[2] = (byte) (this.m_value[2] | 0x80);
        else return;
        this.m_len = 4L;
        dw >>= 7;
        this.m_value[3] = (byte) (int) (dw & 0x7FL);
        if (dw > 127L) this.m_value[3] = (byte) (this.m_value[3] | 0x80);
        else return;
        this.m_len = 5L;
        dw >>= 7;
        this.m_value[4] = (byte) (int) (dw & 0xFL);
    }

    public long ToDWord() {
        long val = 0L;
        long tmp = 0L;
        val = (this.m_value[0] & 0x7F);
        if ((this.m_value[0] & 0x80) == 0) return val;
        tmp = (this.m_value[1] & 0x7F);
        tmp = tmp << 7 & 0x3F80L;
        val |= tmp;
        if ((this.m_value[1] & 0x80) == 0) return val;
        tmp = (this.m_value[2] & 0x7F);
        tmp = tmp << 14 & 0x1FC000L;
        val |= tmp;
        if ((this.m_value[2] & 0x80) == 0) return val;
        tmp = (this.m_value[3] & 0x7F);
        tmp = tmp << 21 & 0xFE00000L;
        val |= tmp;
        if ((this.m_value[3] & 0x80) == 0) return val;
        tmp = (this.m_value[4] & 0xF);
        tmp = tmp << 28 & 0xFFFFFFFFF0000000L;
        return tmp;
    }

    public static boolean ParseDWordEBV(BufferPack pck, TypeEBV ebv) {
        if (pck.readable_length() < 0) return false;
        byte val = pck.ReadData();
        ebv.m_value[0] = val;
        ebv.m_len = 1L;
        if ((val & 0x80) == 0) return true;
        if (pck.readable_length() < 0) return false;
        val = pck.ReadData();
        ebv.m_value[1] = val;
        ebv.m_len = 2L;
        if ((val & 0x80) == 0) return true;
        if (pck.readable_length() < 0) return false;
        val = pck.ReadData();
        ebv.m_value[2] = val;
        ebv.m_len = 3L;
        if ((val & 0x80) == 0) return true;
        if (pck.readable_length() < 0) return false;
        val = pck.ReadData();
        ebv.m_value[3] = val;
        ebv.m_len = 4L;
        if ((val & 0x80) == 0) return true;
        if (pck.readable_length() < 0) return false;
        val = pck.ReadData();
        ebv.m_value[4] = val;
        ebv.m_len = 5L;
        return true;
    }
}
